// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import _ from 'lodash'
import '../semantic/dist/semantic.min.css'
import '../semantic/dist/semantic.min.js'
import ElementUi from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import Icon from 'vue-svg-icon/Icon.vue'
import store from './store'
import { error_code_map } from '@/api'
// 处理日期时间
import moment from 'moment'

window._ = _
Vue.use(ElementUi)
Vue.component('icon', Icon)
Vue.config.productionTip = false
Vue.prototype.$http = axios
Vue.prototype.moment = moment

axios.defaults.timeout = 5000
axios.defaults.baseURL = process.env.API_ROOT

axios.interceptors.request.use(
  config => {
      config.headers['token'] = store.state.user.token || ''
    // 默认情况下所有请求都需要权限
    // config.headers['requireAuth'] = true
      return config
  },
  err => { }
)

// 后端通用错误处理
axios.interceptors.response.use(
  res => {
      if (res.data.result && res.data.result == 'success') {
          return res
      }

      if (res.data.error_code && error_code_map.has(res.data.error_code)) {
          let errorObj = error_code_map.get(res.data.error_code)
          switch (errorObj.action) {
          case 'redirect':
              router.push({path: errorObj.path})
              break
          case 'alert':
              this.$message({
                  message: errorObj.message,
                  type: 'warning'
              })
              break
          case 'hint':
              res.data.hint = errorObj.message
              break
          default:
              break
          }
      }
      return res
  }
)

new Vue({
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>'
})
